package org.example.sdpback.utils;

import org.springframework.util.DigestUtils;

import java.util.Random;

/**
 * @ProjectName: sdp-back
 * @Package: org.example.sdpback.utils
 * @ClassName: MD5Utils
 * @Author: zhuxingyue
 * @Description: MD5加密解密
 * @Date: 2024/11/1 10:40
 * @Version: 1.0
 **/
public class MD5Util {
    private static final String password = "123456";

    /**
     * 加密
     * @param str
     * @param salt
     * @return
     */
    public static String md5(String str, String salt) {
        return DigestUtils.md5DigestAsHex((str + salt).getBytes());
    }

    /**
     * 初始密码
     * @return
     */
    public static String initPassword(String salt) {
        return DigestUtils.md5DigestAsHex((password + salt).getBytes());
    }

    /**
     * 获取盐
     * @return
     */
    public static String getSalt() {
        Random random = new Random();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 2; i++) {
            //随机数字
            char i1 = (char) random.nextInt(48,57);
            //随机小写字母
            char i2 = (char) random.nextInt(65,90);
            //随机大写字母
            char i3 = (char) random.nextInt(97,122);
            stringBuilder.append(i1);
            stringBuilder.append(i2);
            stringBuilder.append(i3);
        }

        return stringBuilder.toString();
    }

}
